package com.glowdayz.glowmee;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.glowdayz.common.ActivityManager;
import com.glowdayz.model.UserInfo;

public class Index extends FragmentActivity implements OnClickListener {
	
	private ActivityManager am = ActivityManager.getInstance();
	
	ImageView bottomBt1 = null, bottomBt2 = null, bottomBt3 = null, bottomBt4 = null, bottomBt5 = null;
	
	ImageView guestBt = null;
	
	LinearLayout bottomLayout = null;
	
	public static boolean isLiskPick = false;
	public static boolean isListProduct = false;
	public static boolean isListReview = false;
	
	String paramType = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_index);
		
		am.addActivity(this);
		
		Intent fromIntent = getIntent();
		paramType = fromIntent.getStringExtra("PARAM_type");
		
		bottomBt1 = (ImageView) findViewById(R.id.bottomBt1);
		bottomBt2 = (ImageView) findViewById(R.id.bottomBt2);
		bottomBt3 = (ImageView) findViewById(R.id.bottomBt3);
		bottomBt4 = (ImageView) findViewById(R.id.bottomBt4);
		bottomBt5 = (ImageView) findViewById(R.id.bottomBt5);
		
		bottomBt1.setOnClickListener(this);
		bottomBt2.setOnClickListener(this);
		bottomBt3.setOnClickListener(this);
		bottomBt4.setOnClickListener(this);
		bottomBt5.setOnClickListener(this);
		
		bottomLayout = (LinearLayout) findViewById(R.id.bottomLayout);
		
		if (UserInfo.getInstance().getUSER_ID() < 1) {
			guestBt = (ImageView) findViewById(R.id.guestBt);
			guestBt.setVisibility(View.VISIBLE);
			
			final Drawable alpha = guestBt.getBackground();
			alpha.setAlpha(230);
			
			guestBt.setOnTouchListener(new OnTouchListener() {
	            @Override
	            public boolean onTouch(View v, MotionEvent event) {
	                switch (event.getAction()) {
	                    case MotionEvent.ACTION_DOWN: {
	                        ImageView view = (ImageView) v;
	                        alpha.setAlpha(250);
	                        view.invalidate();
	                        break;
	                    }
	                    case MotionEvent.ACTION_UP:
	                    case MotionEvent.ACTION_CANCEL: {
	                        ImageView view = (ImageView) v;
	                        alpha.setAlpha(230);
	                        view.invalidate();
	                        break;
	                    }
	                }
	                return false;
	            }
	        });
			guestBt.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Index.this, JoinFirst.class);
					startActivity(intent);
				}
			});
		}
		
		if (paramType != null && paramType.equals("product")) {
			bottomLayout.setVisibility(View.VISIBLE);
    		bottomBt1.setImageResource(R.drawable.bottom_off1);
			bottomBt2.setImageResource(R.drawable.bottom_on2);
			bottomBt3.setImageResource(R.drawable.bottom_off3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(2);
		} else if (paramType != null && paramType.equals("review")) {
			bottomLayout.setVisibility(View.VISIBLE);
    		bottomBt1.setImageResource(R.drawable.bottom_off1);
			bottomBt2.setImageResource(R.drawable.bottom_off2);
			bottomBt3.setImageResource(R.drawable.bottom_on3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(3);
		} else {
			bottomLayout.setVisibility(View.VISIBLE);
    		bottomBt1.setImageResource(R.drawable.bottom_on1);
			bottomBt2.setImageResource(R.drawable.bottom_off2);
			bottomBt3.setImageResource(R.drawable.bottom_off3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(1);
		}
	}
	
	public void fragmentReplace(int reqNewFragmentIndex) {
        Fragment newFragment = null;
        
        switch (reqNewFragmentIndex) {
        case 1:
        	isListProduct = false;
        	isListReview = false;
        	isLiskPick = true;
        	newFragment = new ListPick();
        	break;
        	
        case 2:
        	isListProduct = false;
        	isListReview = false;
        	isLiskPick = false;
        	newFragment = new ListProduct();
        	break;
        	
        case 3:
        	isListProduct = false;
        	isListReview = false;
        	isLiskPick = false;
        	newFragment = new ListReview();
        	break;
        	
        case 4:
        	isListProduct = false;
        	isListReview = false;
        	isLiskPick = false;
        	newFragment = new ListEvent();
        	break;
        	
        case 5:
        	isListProduct = false;
        	isListReview = false;
        	isLiskPick = false;
        	newFragment = new More();
        	break;
        }
        
        final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_fragment, newFragment);
        transaction.commit();
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bottomBt1:
			bottomBt1.setImageResource(R.drawable.bottom_on1);
			bottomBt2.setImageResource(R.drawable.bottom_off2);
			bottomBt3.setImageResource(R.drawable.bottom_off3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(1);
			break;
		case R.id.bottomBt2:
			bottomBt1.setImageResource(R.drawable.bottom_off1);
			bottomBt2.setImageResource(R.drawable.bottom_on2);
			bottomBt3.setImageResource(R.drawable.bottom_off3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(2);
			break;
		case R.id.bottomBt3:
			bottomBt1.setImageResource(R.drawable.bottom_off1);
			bottomBt2.setImageResource(R.drawable.bottom_off2);
			bottomBt3.setImageResource(R.drawable.bottom_on3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(3);
			break;
		case R.id.bottomBt4:
			bottomBt1.setImageResource(R.drawable.bottom_off1);
			bottomBt2.setImageResource(R.drawable.bottom_off2);
			bottomBt3.setImageResource(R.drawable.bottom_off3);
			bottomBt4.setImageResource(R.drawable.bottom_on4);
			bottomBt5.setImageResource(R.drawable.bottom_off5);
			fragmentReplace(4);
			break;
		case R.id.bottomBt5:
			bottomBt1.setImageResource(R.drawable.bottom_off1);
			bottomBt2.setImageResource(R.drawable.bottom_off2);
			bottomBt3.setImageResource(R.drawable.bottom_off3);
			bottomBt4.setImageResource(R.drawable.bottom_off4);
			bottomBt5.setImageResource(R.drawable.bottom_on5);
			fragmentReplace(5);
			break;
		}
	}
	
	// 하드웨어 뒤로가기버튼 이벤트 설정.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			
			if (paramType != null && (paramType.equals("product") || paramType.equals("review") || paramType.equals("pick"))) {
				finish();
				return false;
			}
			
			if (isListProduct) {
				bottomLayout.setVisibility(View.VISIBLE);
	    		bottomBt1.setImageResource(R.drawable.bottom_off1);
				bottomBt2.setImageResource(R.drawable.bottom_on2);
				bottomBt3.setImageResource(R.drawable.bottom_off3);
				bottomBt4.setImageResource(R.drawable.bottom_off4);
				bottomBt5.setImageResource(R.drawable.bottom_off5);
	    		fragmentReplace(2);
	    		return false;
			}
			
			if (isListReview) {
				bottomLayout.setVisibility(View.VISIBLE);
	    		bottomBt1.setImageResource(R.drawable.bottom_off1);
				bottomBt2.setImageResource(R.drawable.bottom_off2);
				bottomBt3.setImageResource(R.drawable.bottom_on3);
				bottomBt4.setImageResource(R.drawable.bottom_off4);
				bottomBt5.setImageResource(R.drawable.bottom_off5);
	    		fragmentReplace(3);
	    		return false;
			}
			
			if (!isLiskPick) {
				bottomLayout.setVisibility(View.VISIBLE);
	    		bottomBt1.setImageResource(R.drawable.bottom_on1);
				bottomBt2.setImageResource(R.drawable.bottom_off2);
				bottomBt3.setImageResource(R.drawable.bottom_off3);
				bottomBt4.setImageResource(R.drawable.bottom_off4);
				bottomBt5.setImageResource(R.drawable.bottom_off5);
	    		fragmentReplace(1);
	    		return false;
	    	}
			break;
		}
    	
    	if (UserInfo.getInstance().getUSER_ID() > 0) {
    		switch (keyCode) {
        	//하드웨어 뒤로가기 버튼에 따른 이벤트 설정
    		case KeyEvent.KEYCODE_BACK:
    			new AlertDialog.Builder(new ContextThemeWrapper(Index.this, android.R.style.Theme_Holo_Light_Dialog))
    			.setTitle("어플 종료")
    			.setMessage("글로우픽을 종료 하시겠습니까?")
    			.setPositiveButton("예", new DialogInterface.OnClickListener() {
    				
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    					//android.os.Process.killProcess(android.os.Process.myPid());
    					moveTaskToBack(true);
    					am.finishAllActivity();
    					finish();
    					System.exit(0);
    				}
    			})
    			.setNegativeButton("아니오", null).show();
    			break;
    		}
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
    @Override
	protected void onDestroy() {
		super.onDestroy();
		am.removeActivity(this);
	}

}
